import 'package:flutter/material.dart';

import 'package:wanandroid_flutter/ui/home_page.dart';
import 'package:wanandroid_flutter/ui/navi_page.dart';
import 'package:wanandroid_flutter/ui/project_page.dart';
import 'package:wanandroid_flutter/ui/system_tree_page.dart';
import 'package:wanandroid_flutter/ui/wxarticle_page.dart';
import 'package:wanandroid_flutter/ui/search_page.dart';

import 'package:wanandroid_flutter/components/drawer_widget.dart';

class BottomNavigationBarWidget extends StatefulWidget {
  @override
  _BottomNavigationBarWidgetState createState() =>
      _BottomNavigationBarWidgetState();
}

class _BottomNavigationBarWidgetState extends State<BottomNavigationBarWidget>
    with AutomaticKeepAliveClientMixin {
  int _currentIndex = 0;
  bool _showAppBar = true;
  bool _showDrawer = true;
  List<Widget> _pages = List();
  List<String> _titleList = [
    "首页",
    "知识体系",
    "公众号",
    "导航",
    "项目",
  ];

  @override
  void initState() {
    _pages
      ..add(HomePage())
      ..add(SystemTreePage())
      ..add(WxArticlePage())
      ..add(NaviPage())
      ..add(ProjectTreePage());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _pages.length,
      child: Scaffold(
        appBar: _showAppBar ? _appBarWidget(context) : null,
        drawer: _showDrawer ? DrawerWidget() : null,
        body: IndexedStack(
          index: _currentIndex,
          children: _pages,
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: _handleTabChanged,
          type: BottomNavigationBarType.fixed,
          fixedColor: Theme.of(context).accentColor,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text('首页'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.library_books),
              title: Text('体系'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.image_aspect_ratio),
              title: Text('公众号'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.navigation),
              title: Text('导航'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.phone_android),
              title: Text('项目'),
            ),
          ],
        ),
      ),
    );
  }

  Widget _appBarWidget(BuildContext context) {
    return AppBar(
      title: Text(_titleList[_currentIndex]),
      elevation: 0.4,
      actions: _actionsWidget(),
    );
  }

  List<Widget> _actionsWidget() {
    if (_showDrawer) {
      return [
        IconButton(
          icon: Icon(Icons.search),
          onPressed: () {
            onSearchClick();
          },
        )
      ];
    } else {
      return null;
    }
  }

  void _handleTabChanged(int value) {
    setState(() {
      _currentIndex = value;
      if (_currentIndex == 0 || _currentIndex == 1 || _currentIndex == 3) {
        _showAppBar = true;
      } else {
        _showAppBar = false;
      }

      if (_currentIndex == 0) {
        _showDrawer = true;
      } else {
        _showDrawer = false;
      }
    });
  }

  void onSearchClick() async {
    await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => SearchPage(),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
